package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.Supplier;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.LabelValueVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.SupplierMapper;
import com.trace.app.service.SupplierService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Supplier)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-28 18:29:31
 */
@Service("supplierService")
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        Page<Supplier> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Supplier> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Supplier supplier) {
        return save(supplier) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Supplier supplier) {
        Supplier oldSupplier = getById(supplier.getId());
        if (supplier.getName() != null) {
            oldSupplier.setName(supplier.getName());
        }
        if (supplier.getEmail() != null) {
            oldSupplier.setEmail(supplier.getEmail());
        }
        if (supplier.getAddress() != null) {
            oldSupplier.setAddress(supplier.getAddress());
        }
        if (supplier.getConnection() != null) {
            oldSupplier.setConnection(supplier.getConnection());
        }
        if (supplier.getPhone() != null) {
            oldSupplier.setPhone(supplier.getPhone());
        }
        if (supplier.getRemark() != null) {
            oldSupplier.setRemark(supplier.getRemark());
        }
        updateById(oldSupplier);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLabelValue() {
        List<LabelValueVo> collect = list().stream().map((category -> {
            LabelValueVo labelValueVo = new LabelValueVo();
            labelValueVo.setValue(String.valueOf(category.getId()));
            labelValueVo.setLabel(category.getName());
            return labelValueVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(name)) {
            queryWrapper.like(Supplier::getName, name);
        }
        Page<Supplier> userPage = new Page<>(pageNum, pageSize);
        page(userPage, queryWrapper);
        PageVo<Supplier> userPageVo = new PageVo<>();
        userPageVo.setTotal(userPage.getTotal());
        userPageVo.setPageTotal(userPage.getPages());
        userPageVo.setResult(userPage.getRecords());

        return ResponseResult.okResult(userPageVo);
    }
}

